﻿using DimensionsHelper.Common.Contracts.Metadata;

namespace DimensionsHelper.Services.Metadata;

public abstract class MetadataObject(IMetadataDocument document, ObjectType objectType) : IMetadataObject
{
    public IProperties Properties { get; } = new Properties(document);

    public IMetadataDocument Document { get; } = document;

    public ObjectType ObjectType { get; protected set; } = objectType;

    public IMetadataObject? Parent { get; internal set; }
        
    public IVersionObjectCollection? Versions { get; internal set; }

    public T? FindParent<T>() where T : IMetadataObject
    {
        return FindParent<T>(this);
    }

    internal static T? FindParent<T>(IMetadataObject obj) where T : IMetadataObject
    {
        var parent = obj.Parent;

        while (parent != null)
        {
            if (parent is T target)
            {
                return target;
            }

            parent = parent.Parent;
        }

        return default;
    }
        
    public bool Versioned { get; set; } = true;


    /// <summary>
    ///     更新版本对象，如果设置失败或者没有重载此方法，返回false。
    /// </summary>
    /// <param name="data">更新的版本数据对象</param>
    /// <returns></returns>
    internal virtual bool SetVersionObjectData(IVersionObject data)
    {
        return false;
    }

}